package com.xyy.ec.pop.erp.hx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.xyy.ec.pop.erp.hx.config.HXTaskConfig;
import com.xyy.ec.pop.erp.hx.enums.TaskEnum;
import com.xyy.ec.pop.erp.hx.po.pop.ApiRPCResult;
import com.xyy.ec.pop.erp.hx.po.pop.dto.ResultDto;
import com.xyy.ec.pop.erp.hx.po.pop.dto.TaskQueryParam;
import com.xyy.ec.pop.erp.hx.service.PopService;
import com.xyy.ec.pop.erp.hx.utils.http.RedirectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PopServiceImpl implements PopService {


    /**
     * 配置信息
     */
    @Autowired
    HXTaskConfig comentCfg;

//    @PostConstruct
//    private void init() {
//        comentCfg = CfgUtils.getAppDefaultCfg();
//    }

    @Override
    public ApiRPCResult<Boolean> pushDataInfo(TaskQueryParam taskQueryParam) {
        TaskEnum value = TaskEnum.findValue(taskQueryParam.getTaskId());
        String pushUrl = comentCfg.getPopApiHost() + comentCfg.getPopPushDataInfo();
        String body = RedirectUtil.doPostByJson(pushUrl, JSON.toJSONString(taskQueryParam));
        ApiRPCResult<Boolean> apiRPCResult = JSON.parseObject(body, ApiRPCResult.class);
        if (comentCfg.getIsOutPutBizLog()) {
            log.info("请求任务: {},请求参数:{},响应参数:{}", value.getTaskName(), taskQueryParam.getJsonData(), JSON.toJSONString(apiRPCResult));
        }
        if (apiRPCResult == null || apiRPCResult.isFail()) {
            log.warn("{} 数据上报失败 , apiRPCResult：{}", value.getTaskName(), JSON.toJSONString(apiRPCResult));
            return new ApiRPCResult<>();
        }
        return apiRPCResult;
    }

    @Override
    public ApiRPCResult<ResultDto> pullDataInfo(TaskQueryParam taskQueryParam) {
        TaskEnum value = TaskEnum.findValue(taskQueryParam.getTaskId());
        String pullUrl = comentCfg.getPopApiHost() + comentCfg.getPopPullDataInfo();
        String body = RedirectUtil.doGetParam(pullUrl, BeanUtil.beanToMap(taskQueryParam, false, true));
        ApiRPCResult<ResultDto> apiRPCResult = JSON.parseObject(body, ApiRPCResult.class);
        if (apiRPCResult == null || apiRPCResult.isFail()) {
            log.warn("{}数据拉取失败 taskQueryParam:{}, apiRPCResult：{}", value.getTaskName(), taskQueryParam, JSON.toJSONString(apiRPCResult));
            return new ApiRPCResult<>();
        }
        if (comentCfg.getIsOutPutBizLog()) {
            log.info("请求任务: {},拉取到数据:{}", value.getTaskName(), JSON.toJSONString(apiRPCResult));
        }
        return apiRPCResult;
    }

    @Override
    public ApiRPCResult<Boolean> callbackForPullTask(TaskQueryParam taskQueryParam) {
        String pullUrl = comentCfg.getPopApiHost() + comentCfg.getPopCallbackForPullTask();
        String body = RedirectUtil.doPostByJson(pullUrl, JSON.toJSONString(taskQueryParam));
        ApiRPCResult<Boolean> apiRPCResult = JSON.parseObject(body, ApiRPCResult.class);
        if (apiRPCResult == null || apiRPCResult.isFail()) {
            log.warn("#订单回调失败 taskQueryParam:{}, apiRPCResult：{}", taskQueryParam, JSON.toJSONString(apiRPCResult));
            return new ApiRPCResult<>();
        }
        return apiRPCResult;
    }

    @Override
    public ApiRPCResult<Boolean> callbackForCustomerPullTask(TaskQueryParam taskQueryParam) {
        String pullUrl = comentCfg.getPopApiHost() + comentCfg.getPopCallbackForCustomerPullTask();
        String body = RedirectUtil.doPostByJson(pullUrl, JSON.toJSONString(taskQueryParam));
        ApiRPCResult<Boolean> apiRPCResult = JSON.parseObject(body, ApiRPCResult.class);
        if (apiRPCResult == null || apiRPCResult.isFail()) {
            log.warn("#客户回调失败 taskQueryParam:{}, apiRPCResult：{}", taskQueryParam, JSON.toJSONString(apiRPCResult));
            return new ApiRPCResult<>();
        }
        log.info("#客户回调成功 taskQueryParam:{}", taskQueryParam);
        return apiRPCResult;
    }

    /**
     * 查询任务执行条件
     *
     * @param taskQueryParam
     * @return
     */
    @Override
    public ApiRPCResult<ResultDto> queryTaskCondition(TaskQueryParam taskQueryParam) {
        TaskEnum value = TaskEnum.findValue(taskQueryParam.getTaskId());
        String pullUrl = comentCfg.getPopApiHost() + comentCfg.getPopQueryTaskCondition();
        String body = RedirectUtil.doGetParam(pullUrl, BeanUtil.beanToMap(taskQueryParam, false, true));
        ApiRPCResult<ResultDto> apiRPCResult = JSON.parseObject(body, ApiRPCResult.class);
        if (apiRPCResult == null || apiRPCResult.isFail()) {
            log.warn("#数据筛选接口查询失败 筛选任务-{} taskQueryParam:{}, apiRPCResul：{}", value.getTaskName(), taskQueryParam, JSON.toJSONString(apiRPCResult));
            return new ApiRPCResult<>();
        }
        return apiRPCResult;
    }
}
